/******************************************************************
 * 文件：main.c
 * 功能：主函数入口
 * 日期：2018-02-16
 * 作者：zx
 * 版本：Ver.1.0 | 最初版本
 * 官博：http://fengmeitech.club
 * Copyright (C) 2017 zx. All rights reserved.
*******************************************************************/
#include "stdio.h"
#include "string.h"
#include "DELAY/Delay.h"
#include "EXTI/EXTI.h"
#include "LED/LED.h"
#include "NVIC/NVIC.h"
#include "ADC/ADC.h"
#include "IIC/IIC.h"
#include "OLED/OLED.h"
#include "RELAY/Relay.h"
#include "DHT11/DHT11.h"
#include "UART/uart.h"
#include "NetWork.h"
#include "TLink.h"
#include "misc.h"
#include "TIMx/TIMx.h"
#include "usart-ultrasonic/usart-ultrasonic.h"
#include "MPU6050/UART_MPU6050.h"
#define MULT 5 //烟雾报警电压峰值5V

void TimerInit(void);
void TIM3_IRQHandler(void);

DeviceSta_Strcture device = {0};

u16 DHT11_data; //存储DHT11传感器采集数据
int n = 0;
int main(void)
{
    int temp = 0;

    /*初始化各外设*/
    initSysTick();
    initUART2(); //用于esp8266: PA2 TX, PA3 RX
    initLED();
    initRelay(); //警示灯 PA5
    initDHT11(); // 温湿度：PA4
    initADC();   //B1
    initIIC();   //OLED 屏幕：B5 SDA    B6 SCL
    initOLED();
    initNVIC(NVIC_PriorityGroup_2); //开启UART2的接收和空闲中断,开启串口3的接收中断

    formatScreen(0x00); //清屏

    /*打印ESP8266启动信息到OLED*/
    if (initESP8266() != 0)
    {
        showString(0, 0, "Init ok!", FONT_16_EN);
        showString(0, 2, "Waiting...", FONT_16_EN);
    }
    else
    {
        showString(0, 0, "Init error!", FONT_16_EN);
    }
    while (connectAP(SSID, PWD) == 0)
    {
        showString(0, 2, "Waiting...", FONT_16_EN);
    }

    formatScreen(0x00); //清屏
    showString(0, 0, "Init ok!", FONT_16_EN);
    showString(0, 2, "Connect AP Ok!", FONT_16_EN);

    if (connectTlink() != 0)
    {
        showString(0, 4, "Connect Link ok!", FONT_16_EN);
    }
    else
    {
        showString(0, 4, "Connect Link no!", FONT_16_EN);
    }
    initUART();  //用于US-100 ：PA9 TX, PA10 RX
    initUART3(); //用于获取陀螺仪角度PB10 TX, PB11 RX
    initTIMx(TIM3, 7199, 9999, TIM_IT_Update, ENABLE);
    TIM_ARRPreloadConfig(TIM3, ENABLE);

    Delay_ms(2000); //让数据保持一段时间
    formatScreen(0x00);

    showString(0, 0, "Hum:", FONT_16_EN);
    showString(70, 0, "Tem:", FONT_16_EN);
    showString(0, 2, "Dis:", FONT_16_EN);
    showString(70, 2, "con:", FONT_16_EN);
    showString(0, 4, "Angle X Y Z:", FONT_16_EN);

    /* 开机先采集一次 */
    DHT11_data = readDHT11();
    Delay_ms(1000);
    device.Humidity = DHT11_data >> 8;
    device.Temperature = DHT11_data & 0x00FF;

    while (1)
    {
        /*  Y(绕此轴旋转形成的角称为俯仰角)
    ^
    |
    |
    |——————> X (绕此轴旋转形成的角称为横滚角)
*/
        //Delay_ms(100);
        toggleLED();
        temp = (13 - (Ultrasonic_ranging_s(5) / 10.0));
        device.Chest = temp > 0 ? temp : 0;
        Get_Mpu6050();
        device.X_Angle = (u8)angle[0];                                             //横滚角
        device.Y_Angle = (u8)angle[1];                                             //俯仰角
        device.Z_Angle = (u8)angle[2];                                             //航向角
        device.Concentration = ((getConvValueAve(10, 1000) * MULT) / 3103.0) * 20; //浓度计算
        showNumber(30, 0, device.Humidity, DEC, 3, FONT_16_EN);
        showNumber(100, 0, device.Temperature, DEC, 3, FONT_16_EN);
        showNumber(30, 2, device.Chest, DEC, 3, FONT_16_EN);
        showNumber(100, 2, device.Concentration, DEC, 3, FONT_16_EN);
        showNumber(0, 6, device.X_Angle, DEC, 4, FONT_16_EN);
        showNumber(40, 6, device.Y_Angle, DEC, 4, FONT_16_EN);
        showNumber(80, 6, device.Z_Angle, DEC, 4, FONT_16_EN);
    }
}

void TIM3_IRQHandler(void)
{

    if (TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)
    {
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
        n++;
        if (n == 2)
        {
            DHT11_data = readDHT11();
            device.Humidity = DHT11_data >> 8;
            device.Temperature = DHT11_data & 0x00FF;
            n = 0;
        }

        sendDeviceStatus(&device); //设备状态上传
    }
}
